<template>
  <div>
    <Card>
      <Button  type="primary" @click="show(newparams)">添加</Button>
      <tables ref="tables" :loading="loading" editable searchable search-place="top" v-model="tableData" :columns="columns" />
      <Button style="margin: 10px 0;" type="primary" @click="exportExcel">导出为Csv文件</Button>
      
    </Card>
    <Modal title="商品类型修改"  v-model="showmodal"  >
            <Form ref="ztype"  :model="params" :rules="ruleInline" :label-width="80">
                <FormItem label="商品类型" prop="name">
                    <Input v-model="params.name" placeholder="请输入商品类型"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button  size="large" @click="cancleSubmit('ztype')">取消</Button>
                <Button type="primary" size="large" @click="save('ztype')">确认</Button>
            </div>
        </Modal>
  </div>
</template>

<script>
import Tables from "_c/tables";
import { getMerchantsType,typeSave } from "@/api/merchants";
import MerchantsTypeForm from "./merchants-type-form";
export default {
  name: "tables_page",
  components: {
    Tables,
    MerchantsTypeForm
  },
  data() {
    return {
      columns: [
        {
          title: "商户类型",
          key: "name",
          searchable: false
        },
        {
          title: "操作",
          key: "action",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    size: "small",
                    type: "success"
                  },
                  on: {
                    click: () => {
                      this.show(params.row);
                    }
                  }
                },
                "编辑"
              )
            ]);
          }
        }
      ],
      tableData: [],
      totalPage: 0,
      size: 10,
      loading: true,
      params: {},
      newparams:{name:""},
      showmodal: false,
      ruleInline: {
        name: [{ required: true, message: "请输入商品类型", trigger: "blur" }]
      }
    };
  },
  methods: {
    exportExcel() {
      this.$refs.tables.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      });
    },
    changePage(e) {
      const page = e;
      const size = this.size;
      getMerchantsType().then(res => {
        this.tableData = res.data;
        this.loading = false;
      });
    },
    show(params1) {
      this.showmodal = true;
      this.params = params1;
    },
    cancleSubmit(name){
            this.$refs[name].resetFields();
    },    
    save(name){
        var that=this
         this.$refs[name].validate((valid) => {
                    if (valid) {
                      
                        typeSave(this.params).then(res=>{
                           this.showmodal=false;
                          that.$Message.info('创建成功！')
                        })
                    }
                })
    }
  },
  mounted() {
    const page = 1;
    const size = this.size;
    getMerchantsType().then(res => {
      this.tableData = res.data;
      this.loading = false;
    });
  }
};
</script>

<style>
</style>
